import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import 'package:get/get.dart';
import 'package:kq_flutter_core_widget/widgets/expandablePageView/kq_expandable_page_view.dart';
import 'package:kq_flutter_widgets/resources/kq_theme_colors.dart';
import 'package:kq_flutter_core_widget/widgets/button/kq_ink_well.dart';
import 'package:kq_flutter_core_widget/widgets/image/kq_image.dart';
import 'package:kq_flutter_core_widget/widgets/imageBox/kq_image_box.dart';
import 'package:kq_flutter_widgets/widgets/formItem/entity/kq_form_entity.dart';
import 'package:kq_flutter_widgets/widgets/formItem/kq_attachment_view.dart';
import 'package:kq_flutter_widgets/widgets/formItem/kq_header_item_view.dart';
import 'package:kq_flutter_widgets/widgets/imageBox/kq_head_image_view.dart';
import 'package:kq_flutter_widgets/widgets/titleBar/kq_title_bar.dart';
import 'package:kq_flutter_widgets/widgets/wrapFlow/kq_auto_column_flow.dart';
import 'package:kq_flutter_widgets_example/resources/Images.dart';

/// 自定义控件演示页面
class CustomExpandView extends StatefulWidget {
  const CustomExpandView({super.key});

  @override
  State<StatefulWidget> createState() {
    return CustomExpandViewState();
  }
}

class CustomExpandViewState extends State<CustomExpandView> {
  KqFormEntity imgEntity =
      KqFormEntity(title: '上传图片', editable: true, imageUrls: [
    ImageUrl(
        imageType: ImageType.http,
        url: 'https://imgs.699pic.com/01/500/628/783/500628783.jpg!list2x.v1')
  ]);
  bool isBom = false;
  PageController pageController = PageController();
  int pageIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: KqHeadBar(
          headTitle: "自定义控件演示2",
          back: () => Get.back(),
        ),
        body: SingleChildScrollView(
          padding: EdgeInsets.symmetric(horizontal: 20.r, vertical: 20.r),
          child: Column(
            children: [
              KqHeadImageView(
                entity: imgEntity,
                defaultUrl: ImageUrl(
                    imageType: ImageType.assets, url: Images.remakeIcAddRemake),
                didSelected: (file) {
                  setState(() {
                    imgEntity.imageUrls = [
                      ImageUrl(imageType: ImageType.file, url: file.filePath!)
                    ];
                  });
                },
              ),
              SizedBox(height: 10.r),
              KqHeadImageView(
                entity: imgEntity,
                defaultUrl: ImageUrl(
                    imageType: ImageType.assets, url: Images.remakeIcAddRemake),
                isCircle: true,
                isOriginal: true,
                gradient: const LinearGradient(
                    begin: Alignment.topLeft,
                    end: Alignment.bottomLeft,
                    stops: [0.0, 1.0],
                    colors: [KqThemeColors.textBlue, KqThemeColors.bgWhite]),
              ),
              SizedBox(height: 10.r),
              KqAttachmentView(
                onTap: (item, index) {},
                entity: KqFormEntity(
                  title: '附件信息',
                  extraData: [
                    KqFromAttachEntity(attachmentName: 'xssssss.docx'),
                    KqFromAttachEntity(
                      attachmentName:
                          'jdhbesdfrefeferefefefefefewfwewefwefwefwefewf.xls',
                    )
                  ],
                ),
              ),
              SizedBox(height: 10.r),
              Container(
                color: Colors.white,
                padding: EdgeInsets.symmetric(vertical: 12.r, horizontal: 16.r),
                child: KqAutoColumnFlow(
                  backgroundColor: KqThemeColors.bgD,
                  rowCountCallback: () {
                    return 5;
                  },
                  totalCount: 9,
                  renderCrossAxisCountCallback: ((row) {
                    if (row == 0) {
                      return 1;
                    } else if (row == 1) {
                      return 3;
                    }
                    return 2;
                  }),
                  renderIconPathCallback: (row, column) {
                    if (column == 0) {
                      return Images.homeIcBanbenhao;
                    }
                    return Images.demoIcChoice16Sel;
                  },
                  renderTextCallback: ((row, column) {
                    if (column == 0 && row == 2) {
                      return '';
                    }
                    return '2023-09-09 13:59';
                  }),
                  renderWidgetCallback: (row, column) {
                    if (column == 0 && row == 2) {
                      return Expanded(
                          child: RichText(
                              maxLines: 1,
                              text: TextSpan(text: 'dddd', children: [
                                TextSpan(
                                    text: 'ssssssssssssssssssssxxxxxxxxx',
                                    style: TextStyle(
                                        color: Colors.red, fontSize: 30.sp))
                              ])));
                    }
                    return null;
                  },
                  renderTextMaxLineCallback: (row, column) {
                    return 1;
                  },
                ),
              ),
              SizedBox(height: 10.r),
              KqHeaderItemView(entity: KqFormEntity(title: '高度随子项改变的PageView')),
              Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  KqInkWell(
                      onTap: () {
                        // 左切
                        if (pageIndex > 0) {
                          pageIndex--;
                          pageController.jumpToPage(pageIndex);
                        }
                      },
                      child: Container(
                        width: 38.r,
                        height: 38.r,
                        padding: EdgeInsets.all(10.r),
                        child: Text(
                          pageIndex > 0 ? "<" : "",
                          style: TextStyle(
                              fontSize: 16.r, color: KqThemeColors.text26),
                        ),
                      )),
                  Expanded(
                    child: KqInkWell(
                        onTap: () {
                          setState(() {
                            isBom = !isBom;
                            pageIndex = 0;
                          });
                        },
                        child: Container(
                          alignment: Alignment.center,
                          padding: EdgeInsets.all(10.r),
                          child: Text(
                            "切换",
                            style: TextStyle(
                                fontSize: 16.r, color: KqThemeColors.text26),
                          ),
                        )),
                  ),
                  KqInkWell(
                      onTap: () {
                        // 右切
                        if (pageIndex < getPageList().length - 1) {
                          pageIndex++;
                          pageController.jumpToPage(pageIndex);
                        }
                      },
                      child: Container(
                        width: 38.r,
                        height: 38.r,
                        padding: EdgeInsets.all(10.r),
                        child: Text(
                          pageIndex < getPageList().length - 1 ? ">" : "",
                          style: TextStyle(
                              fontSize: 16.r, color: KqThemeColors.text26),
                        ),
                      )),
                ],
              ),
              KqExpandablePageView(
                physics: const NeverScrollableScrollPhysics(),
                duration: const Duration(milliseconds: 0),
                pageController: pageController,
                children: getPageList(),
                onPageChanged: (index) {
                  setState(() {
                    pageIndex = index;
                  });
                },
              ),
              SizedBox(height: 10.r),
              KqExpandablePageView(
                children: getPageList(),
              ),
              SizedBox(height: 10.r),
              SizedBox(height: 200.r),
            ],
          ),
        ));
  }

  List<Widget> getPageList() {
    List<Widget> list2 = [
      Container(
        width: double.infinity,
        height: 100.r,
        color: KqThemeColors.textRed,
        alignment: Alignment.center,
        child: Text(
          "第1页",
          style: TextStyle(fontSize: 30.r, color: Colors.white),
        ),
      ),
      Container(
        width: double.infinity,
        height: 50.r,
        color: KqThemeColors.textGreen,
        alignment: Alignment.center,
        child: Text(
          "第2页",
          style: TextStyle(fontSize: 30.r, color: Colors.white),
        ),
      ),
      Container(
        width: double.infinity,
        height: 150.r,
        color: KqThemeColors.textBlue,
        alignment: Alignment.center,
        child: Text(
          "第3页",
          style: TextStyle(fontSize: 30.r, color: Colors.white),
        ),
      )
    ];
    List<Widget> list1 = [
      Container(
        width: double.infinity,
        height: 100.r,
        color: KqThemeColors.text26,
        alignment: Alignment.center,
        child: Text(
          "第1页",
          style: TextStyle(fontSize: 30.r, color: Colors.white),
        ),
      ),
    ];
    return isBom ? list2 : list1;
  }
}
